﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Entities;
using Context;
using System.IO;
namespace WCF_ListenMusic.Services.Module_Song.User
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SongService" in code, svc and config file together.
     [ServiceBehavior(TransactionAutoCompleteOnSessionClose=true)]
    public class SongService : ISongService
    {
        Connect_DBDataContext context = new Connect_DBDataContext();        
        #region ISongService Members
       [OperationBehavior(TransactionAutoComplete= true,TransactionScopeRequired=true)]
        public  bool UploadSong(Song s)
        {
            context.CommandTimeout = 3600;
            try
            {
                s.SongID = getMax_SongID() + 1;
                context.Songs.InsertOnSubmit(s);
                context.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region ISongService Members


        public int getMax_SongID()
        {
            try
            {

                var q = (from p in context.Songs
                         select (int?)p.SongID).Max() ?? 0;
                return q;

            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion






        public bool UpdateLyrics(int songid, string lyrics)
        {
            try
            {
                var q = (from a in context.Songs
                        where a.SongID.Equals(songid)
                        select a).First();
                q.Lyrics = lyrics;
                context.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw new FaultException(ex.Message);
            }
        }
    }
}
